<template>
  <div>
    <el-card class="box-construction" shadow="always">
      <div class="box-title box-title-position">
        <div>角色管理</div>
        <el-button
          class="add-btn"
          size="medium"
          type="primary"
          v-permission="['roles:add']"
          @click="addClassic()"
          >新增角色</el-button
        >
      </div>
      <el-table
        v-loading="loading"
        :header-cell-style="{ background: '#eef1f6', color: '#606266' }"
        :data="tableData"
        border
        style="width: 100%; margin-top: 25px"
      >
        <el-table-column
          :show-overflow-tooltip="true"
          prop="name"
          label="角色名称"
        ></el-table-column>
        <!-- <el-table-column
          :show-overflow-tooltip="true"
          prop="status"
          label="状态"
          width="130"
        >
          <template slot-scope="scope">
            <span
              @click="markCheck(scope.row)"
              :style="{
                color: scope.row.status == '1' ? '#409eff' : 'red',
                cursor: 'pointer',
              }"
              style=""
              >{{ scope.row.status == 1 ? "启用" : "禁用" }}</span
            >
          </template>
        </el-table-column> -->
        <el-table-column prop="createTime" label="创建时间"></el-table-column>
        <el-table-column prop="version" label="操作" width="400">
          <template slot-scope="scope">
            <!-- <el-button
              size="small"
              icon="el-icon-tickets"
              @click="bindRule(scope.row)"
              >绑定规则</el-button
            >
            <el-button
              size="small"
              icon="el-icon-user"
              @click="bindUser(scope.row)"
              >绑定用户</el-button
            > -->
            <el-button
              size="small"
              icon="el-icon-edit"
              @click="editClassic(scope.row)"
              >编辑</el-button
            >
            <el-button
              size="small"
              icon="el-icon-delete"
              v-permission="['roles:delete']"
              @click="delClsaaic(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        class="box-page"
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="pagination.currentPage"
        :page-sizes="[5, 10, 20, 30, 40]"
        :page-size="pagination.pageSize"
        layout="total,sizes, prev, pager, next"
        :total="pagination.total"
      ></el-pagination>
    </el-card>
    <!-- :datas.sync="editRow" -->
    <EditForm
      ref="editForm"
      :title="formTitle"
      
      :visible.sync="visible"
      :baseStyle="baseStyle"
      @saveForm="submit"
    />
    <BindRole
      ref="bindRole"
      :showProp.sync="showProp"
      :data.sync="roleDatas"
      :bindValue.sync="bindValue"
      :title="formTitle"
      :transferTitle="transferTitle"
      :visible.sync="visibleBind"
      @saveForm="submitBind"
    />
  </div>
</template>

<script>
import orgAPI from "@apis/organize";
import rightsSetAPI from "@apis/rightsSet";
import EditForm from "./editForm/index";
import BindRole from "../commonTransfer/index";
export default {
  data() {
    return {
      loading: false,
      tableData: [],
      // editRow: {},
      visible: false,
      baseStyle: {
        width: "140px",
      },
      isAdd: true,
      formTitle: "新增角色",
      visibleBind: false,
      roleDatas: [],
      transferTitle: [],
      bindValue: [],
      showProp: {},
      rulesDatas: [],
      usersDatas: [],

      pagination: {
        currentPage: 1,
        pageNo: 1,
        pageSize: 5,
        total: 0,
      },
    };
  },
  components: {
    EditForm,
    BindRole,
  },
  created() {
    this.loading = true;
    this.getList(1);
    // this.getrulesData();
    // this.getusersData();
  },
  mounted() {},
  methods: {
    getList(val = 1) {
      let parmars = {
        pageNo: val,
        pageSize: this.pagination.pageSize,
      };
      rightsSetAPI
        .getRolesList(parmars)
        .then((res) => {
          this.pagination.total = res.totalElements
          this.tableData = [...res.content]
          this.loading = false
        })
        .catch((err) => {
          this.loading = false;
          // if (err.data.code) {
            this.$message.error(err.message);
          // }
        });
    },
    //控制每页条数
    handleSizeChange(val) {
      this.pagination.pageSize = val;
      this.loading = true;
      this.getList(this.pagination.pageNo);
    },
    //分页
    handleCurrentChange(val) {
      this.loading = true;
      this.getList(val);
    },

    getrulesData() {
      this.rulesDatas = [];
      rightsSetAPI
        .getPolicyList({ pageNo: 1, pageSize: 1000 })
        .then((res) => {
          this.rulesDatas = [...res.data];
        })
        .catch((err) => {
          this.$message.error(err.message);
        });
    },
    bindRule(val) {
      this.roleDatas = [...this.rulesDatas];
      this.showProp = {
        key: "policy_id",
        label: "policyname",
      };
      this.transferTitle = ["规则列表", "已选规则"];
      this.formTitle = "规则";
      //   this.bindValue=[1,2];//默认勾选数据
      this.bindValue = [];
      this.visibleBind = true;

      // this.roleDatas = [];
      // this.loading = true;
      // orgAPI
      //   .userList({ pageNo: 1, pageSize: 1000 })
      //   .then((res) => {
      //     this.showProp = {
      //       key: "id",
      //       label: "realname",
      //     };
      //     this.roleDatas = [...res.data];
      //     this.transferTitle = ["规则列表", "已选规则"];
      //     this.formTitle = "规则";
      //     //   this.bindValue=[1,2];//默认勾选数据
      //     this.bindValue = [];
      //     this.visibleBind = true;
      //     this.loading = false;
      //   })
      //   .catch((err) => {
      //     this.loading = false;
      //     this.$message.error(err.message);
      //   });
    },
    getusersData() {
      this.usersDatas = [];
      orgAPI
        .userList({ pageNo: 1, pageSize: 1000 })
        .then((res) => {
          this.usersDatas = [...res.data];
        })
        .catch((err) => {
          this.$message.error(err.message);
        });
    },
    bindUser(val) {
      this.roleDatas = [...this.usersDatas];
      this.showProp = {
        key: "id",
        label: "realname",
      };
      this.transferTitle = ["用户列表", "已选用户"];
      this.formTitle = "用户";
      //   this.bindValue=[1,2];//默认勾选数据
      this.bindValue = [];
      this.visibleBind = true;
    },
    addClassic() {
      this.formTitle = "新增角色";
      this.$refs.editForm.setData({})
      this.visible = true;
      this.isAdd = true;
      // this.editRow = {};
    },
    editClassic(row) {
      this.formTitle = "编辑角色";
      this.$refs.editForm.setData({...row})
      // this.editRow = {...row}
      this.visible = true;
      this.isAdd = false;
    },
    delClsaaic(data) {
      this.$confirm("删除角色, 是否继续?", "提示", {
        closeOnClickModal: false,
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          if (this.pagination.pageNo > 1 && this.tableData.length == 1) {
            this.pagination.pageNo = this.pagination.pageNo - 1;
          }
          this.loading = true;
          rightsSetAPI
            .rolesDel([data.roleId])
            .then((res) => {
              // this.getList(this.pagination.pageNo);
              this.getList(this.pagination.currentPage)
            })
            .catch((err) => {
              this.$message.error(err.message);
            })
            .finally(()=>{
              this.loading = false
            });
        })
        .catch(() => {});
    },
    //标记域状态
    markCheck(row) {
      this.$confirm("修改角色状态, 是否继续?", "提示", {
        closeOnClickModal: false,
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let status = row.status == 1 ? -1 : 1;
          this.loading = true;
          rightsSetAPI
            .rolesSetStatus({ roles_id: row.roles_id, status })
            .then((res) => {
              this.getList(this.pagination.pageNo);
            })
            .catch((err) => {
              this.$message.error(err.message);
            });
        })
        .catch(() => {});
    },
    submit(val) {
      if (this.isAdd) {
        rightsSetAPI
          .rolesAdd({ ...val })
          .then((res) => {
            this.$refs.editForm.cancel();
            this.loading = true;
            this.getList(1);
          })
          .catch((err) => this.$message.error(err.message));
      } else {
        rightsSetAPI
          .rolesEdit({ ...val })
          .then((res) => {
            this.$refs.editForm.cancel();
            this.loading = true;
            this.getList(this.pagination.currentPage);
          })
          .catch((err) => this.$message.error(err.message));
      }
    },
    submitBind(val) {
      console.log(val, "bindValue");
      this.bindValue = [];
      this.$refs.bindRole.cancel();
    },
  },
};
</script>

<style lang="scss" scoped>
.box-title-position {
  position: relative;
  .add-btn {
    position: absolute;
    right: 0;
    top: -3px;
  }
}
.box-construction {
  min-height: 100px;
  .box-page {
    text-align: right;
    padding-top: 40px;
  }
}
</style>